package com.taotao.web;

import com.taotao.TaotaoResult;
import com.taotao.ext.TbItemExt;
import com.taotao.service.CartService;
import com.taotao.service.TbItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private TbItemService tbItemService;


    /**
     * 展示购物车中的数据
     * @param model
     * @param req
     * @return
     */
    @RequestMapping("/cart")
    public String showCart(Model model, HttpServletRequest req) {
        TaotaoResult taotaoResult = cartService.showCart(req);
        model.addAttribute("cartList", taotaoResult.getData());
        return "cart";
    }

    /**
     * 添加商品到购物车
     * @param id
     * @param model
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping("/add/{id}")
    public String addProduct2Cart(@PathVariable("id") Long id, Model model, HttpServletRequest req, HttpServletResponse resp) {

        // 根据商品id查询商品详细信息
        TbItemExt tbItem = tbItemService.getTbItemInfo(id);

        TaotaoResult taotaoResult = cartService.addItem(tbItem, req, resp);
        model.addAttribute("cartList", taotaoResult.getData());
        return "cart";
    }

}
